﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using Blueprints.Editing.Actions;
using Blueprints.Validation;
using System.Collections.Generic;

namespace Blueprints.Editing
{
    /// <summary>
    /// A property of an object node.
    /// </summary>
    public sealed class EditProperty
    {
        private readonly EditContext editContext;

        internal EditProperty(string name, EditNode declaringNode)
        {
            editContext = declaringNode.EditContext;
            DeclaringNode = declaringNode;
            Name = name;
        }

        /// <summary>
        /// Gets the node that declares this property.
        /// </summary>
        public EditNode DeclaringNode
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the name of the property.
        /// </summary>
        public string Name
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets whether this property is required.
        /// </summary>
        public bool IsRequired
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets an indication on how this property is to be edited.
        /// </summary>
        public EditType EditType
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the node containing the property value, or null if the property has no value.
        /// </summary>
        public EditNode Node
        {
            get;
            internal set;
        }

        /// <summary>
        /// Gets the property type.
        /// </summary>
        public string Type
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the available values for this property, if this is a value property.
        /// </summary>
        public IReadOnlyDictionary<ulong, string> AvailableValues
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates a node for the property value.
        /// </summary>
        /// <returns>The created node.</returns>
        public EditNode CreateNode()
        {
            return DeclaringNode.EditContext.Do(new CreatePropertyNodeAction(DeclaringNode, Name));
        }

        /// <summary>
        /// Creates a node for the property value.
        /// </summary>
        /// <param name="value">The value to assign to the node.</param>
        /// <returns>The created node.</returns>
        public EditNode CreateNode(string value)
        {
            return DeclaringNode.EditContext.Do(new CreatePropertyNodeAction(DeclaringNode, Name, value));
        }

        /// <summary>
        /// Removes the node that contains the property value.
        /// </summary>
        /// <returns>True if the node was removed, false if the property had no value.</returns>
        public bool RemoveNode()
        {
            if (Node == null)
            {
                return false;
            }

            DeclaringNode.EditContext.Do(new RemoveNodeAction(Node, DeclaringNode));
            return true;
        }

        internal void SetValidation(IValidatedProperty property)
        {
            IsRequired = property.IsRequired;
            EditType = property.EditType;
            AvailableValues = property.AvailableValues;
            Type = property.Type;
            Node = editContext.GetEditNode(property.Node);
        }
    }
}